package icu.twtool.ui.router.routes.exercise

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.onClick
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.RadioButton
import androidx.compose.material.Slider
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerHoverIcon
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogWindowScope
import androidx.compose.ui.window.rememberDialogState
import icu.twtool.database.mapper.ExerciseMeteDataMapper
import icu.twtool.database.model.ExerciseMetaData
import icu.twtool.ui.component.TertiaryTextButton
import icu.twtool.ui.style.MyColors
import icu.twtool.ui.style.MyCursor
import icu.twtool.ui.style.MyShape

/**
 * @author Wen
 * @since 2022-11-03
 */
@Composable
@Suppress("FunctionName")
fun AddExerciseDialog(onCloseRequest: () -> Unit, onComplete: () -> Unit) {
    val dialogState = rememberDialogState(height = 400.dp)
    Dialog(onCloseRequest = onCloseRequest, transparent = true, undecorated = true, resizable = false, state = dialogState) {
        Box(Modifier.padding(16.dp)) {
            Surface(
                elevation = 10.dp,
                shape = MyShape.RoundedCornerLarge,
                color = MyColors.SecondaryBackground,
                contentColor = MyColors.OnSecondaryBackground
            ) {
                Column {
                    AddExerciseDialogTitle()
                    AddExerciseDialogContent(onCloseRequest, onComplete)
                }
            }
        }
    }
}

@Composable
@Suppress("FunctionName")
fun DialogWindowScope.AddExerciseDialogTitle() {
    WindowDraggableArea {
        Box(Modifier.fillMaxWidth().padding(start = 16.dp, end = 16.dp, top = 16.dp, bottom = 16.dp)) {
            Text("添加习题", style = MaterialTheme.typography.h5, fontWeight = FontWeight.Bold)
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
@Suppress("FunctionName")
private fun AddExerciseDialogContent(onCloseRequest: () -> Unit, onComplete: () -> Unit) {
    var name by remember { mutableStateOf("") }
    var type by remember { mutableStateOf("混合") }
    var equationCount by remember { mutableStateOf(50) }
    var equationCountFloat by remember { mutableStateOf(equationCount.toFloat()) }
    Column(Modifier.padding(16.dp)) {
        OutlinedTextField(name, onValueChange = { name = it }, modifier = Modifier.fillMaxWidth(),
            placeholder = {
                Text("输入习题的名称")
            }, label = {
                Text("习题名")
            }
        )
        Spacer(Modifier.requiredHeight(16.dp))
        Text("习题类型")
        Row(horizontalArrangement = Arrangement.SpaceAround, modifier = Modifier.fillMaxWidth().padding(8.dp)) {
            for (item in ExerciseMetaData.Type.values()) {
                Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.pointerHoverIcon(MyCursor.HandCursor).onClick { type = item.type }) {
                    RadioButton(type == item.type, onClick = null)
                    Spacer(Modifier.requiredWidth(4.dp))
                    Text(item.type)
                }
            }
        }
        Spacer(Modifier.requiredHeight(16.dp))
        Text("习题数：$equationCount")
        Slider(
            equationCountFloat,
            onValueChange = {
                equationCountFloat = it
            },
            onValueChangeFinished = {
                equationCountFloat = (equationCountFloat.toInt() / 50 * 50).toFloat()
                if (equationCountFloat < 50) equationCountFloat = 50f
                equationCount = equationCountFloat.toInt()
            },
            valueRange = 0f..500f, steps = 9
        )

        Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
            TertiaryTextButton("取消") {
                onCloseRequest()
            }
            Spacer(Modifier.requiredWidth(8.dp))
            TertiaryTextButton("添加") {
                if (ExerciseMeteDataMapper.insert(name, type, equationCount)) {
                    onComplete()
                }
            }
        }
    }
}